package cn.hiapi.shopkeeper.service.order;

import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.entity.order.ActivityGroupOrder;
import cn.hiapi.shopkeeper.jpa.order.ActivityGroupOrderJpa;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author AdinZ
 * @date 2022/12/30 22:23
 */
@Service
public class ActivityGroupOrderService extends AbsBasicService<ActivityGroupOrderJpa, ActivityGroupOrder, Long> {

    private final DispatchContext dispatchContext;

    public ActivityGroupOrderService(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }


    /**
     * 新团支付成功回调状态
     *
     * @param id
     */
    @Transactional
    public void newGroupCallback(long id) {
        ActivityGroupOrder order = this.get(id);
        if (order == null) throw new BasicException("团购单不存在");
        if (order.getStatus() != 0) throw new BasicException("团购单状态错误");
        order.setStatus(1);
        //超时未成团时间
        order.setExpire(System.currentTimeMillis() + (order.getTimeout() * 60L * 1000L));
        order.setUpdated(System.currentTimeMillis());
        order.setBuyNum(1);
        this.update(order);
    }

    /**
     * 加入团购支付回调
     *
     * @param id
     */
    @Transactional
    public void joinGroupCallback(long id) {
        ActivityGroupOrder order = this.get(id);
        if (order == null) throw new BasicException("团购单不存在");
        if (order.getStatus() != 1) throw new BasicException("团购单状态错误");
        order.setUpdated(System.currentTimeMillis());
        order.setBuyNum(order.getBuyNum() + 1);
        if (order.getBuyNum() >= order.getGroupNum()) {
            order.setStatus(2);
            //成团成功
            this.dispatchContext.getServiceOne(ActivityOrderService.class).groupSuccess(id);
        }
        this.update(order);
        this.redisService.increment(AppConst.ACTIVITY_SELL_STOCK_KEY, order.getId(), -1L);
    }

    @Transactional
    public void dispatch(String type, long id) {
        switch (StringUtils.toRootLowerCase(type)) {
            case AppConst.GROUP_NEW:
                this.newGroupCallback(id);
                break;
            case AppConst.GROUP_JOIN:
                this.joinGroupCallback(id);
                break;
            case AppConst.GROUP_CLOSE:
                this.redisService.increment(AppConst.ACTIVITY_SELL_STOCK_KEY, id, -1L);
                this.closeGroup(id);
                break;
        }
    }

    /**
     * 团长未支付的时候 订单关闭时  关闭团购
     *
     * @param id
     */
    @Transactional
    public void closeGroup(long id) {
        ActivityGroupOrder groupOrder = this.get(id);
        if (groupOrder.getStatus() != 0) return;
        groupOrder.setStatus(-1);
        groupOrder.setUpdated(System.currentTimeMillis());
        this.save(groupOrder);
    }


    public long getParticipate(long aid) {
        return this.count(new QueryWrapper().eq("aid", aid));
    }
}
